package me.exchange.com.utils;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

public class BeanUtil {

    /**
     * 合并对象
     * @param
     * @return
     */
    public static void getNullProperties(Object o1,Object o2){
        BeanWrapper srcBean=new BeanWrapperImpl(o1);
        PropertyDescriptor[] pds=srcBean.getPropertyDescriptors();
        Set<String> emptyName=new HashSet<>();
        for(PropertyDescriptor p:pds){
            Object srcValue=srcBean.getPropertyValue(p.getName());
            if(srcValue==null) emptyName.add(p.getName());
        }
        String[] result=new String[emptyName.size()];
        BeanUtils.copyProperties(o1,o2,emptyName.toArray(result));
    }

    public static Object merge(Object master, Object follow){

        if(master ==null && follow ==null)

            return null;

        else if(master == null && null != follow)

            return follow;

        else if(null != master && null == follow)

            return master;

        else{

            Object[] objectNone =new Object[]{};

            Class<?> c1 =master.getClass();

            Field[] fields =c1.getDeclaredFields();

            Method[] methods =c1.getDeclaredMethods();

            for(Field f : fields){

                String fName =f.getName();

                Object o1 =null;

                Object o2 =null;

                for(Method m : methods){

                    String mName =m.getName();

                    if(mName.contains("get") &&mName.equalsIgnoreCase("get"+fName)){

                        try {

                            o1 = m.invoke(master, objectNone);

                            o2 = m.invoke(follow, objectNone);

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                        break;

                    }

                }

                o1 = getFitObject(o1,o2);

                for (Method m : methods) {

                    String mName =m.getName();

                    if (mName.contains("set") &&mName.equalsIgnoreCase("set" +fName)) {

                        try {

                            m.invoke(master,new Object[] { o1 });

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                        break;

                    }

                }

            }

        }
        return master;
    }





    private static Object getFitObject(Object o1, Object o2){

        if(null == o1 && null == o2){

            return o1;

        }else if(null != o1 && null == o2){

            return o1;

        }else if(null == o1 && null != o2){

            return o2;

        }else{

            String s1 = o1.toString();

            String s2 = o2.toString();

            if(s1.equals("0"))//过滤那些int long 

                s1 = "";

            if(StringUtils.isBlank(s1) && StringUtils.isBlank(s2)){

                return o1;

            }else if(StringUtils.isNotBlank(s1) && StringUtils.isBlank(s2)){

                return o1;

            }else if(StringUtils.isBlank(s1) && StringUtils.isNotBlank(s2)){

                return o2;

            }else{

                return o1;

            }

        }

    }
}
